
package com.windgo.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.windgo.bean.MarketComment;
import com.windgo.bean.MarketCommentExample;
import com.windgo.bean.MarketTopic;
import com.windgo.bean.MarketUser;
import com.windgo.bean.common.BasePageInfo;
import com.windgo.bean.common.CommonData;
import com.windgo.bean.vo.WxCommentCountVo;
import com.windgo.bean.vo.WxCommentListVo;
import com.windgo.bean.vo.WxUserInfoOfCommentVo;
import com.windgo.mapper.MarketCommentMapper;
import com.windgo.mapper.MarketTopicMapper;
import com.windgo.mapper.MarketUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author:ZhouXin
 * @Date:2022/10/12 18:37
 * @Description:
 **/
@Service
public class CommentServiceImpl implements CommentService{
    @Autowired
    MarketCommentMapper marketCommentMapper;

    @Autowired
    MarketUserMapper marketUserMapper;

    @Autowired
    MarketTopicMapper marketTopicMapper;



    /**
     * 显示所有的商品评论
     * @param info
     * @param userId
     * @param valueId
     * @return
     */
    @Override
    public CommonData<MarketComment> list(BasePageInfo info, Integer userId, Integer valueId) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        MarketCommentExample example = new MarketCommentExample();

        //排序
        example.setOrderByClause(info.getSort()+" "+info.getOrder());

        MarketCommentExample.Criteria criteria = example.createCriteria();

        //做判断
        if (userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId!=null){
            criteria.andValueIdEqualTo(valueId);
        }
        List<MarketComment> list = marketCommentMapper.selectByExample(example);

        PageInfo<MarketComment> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }


    /**
     * 根据id删除评评论
     * @param id
     */
    @Override
    public void delete(Integer id) {
        marketCommentMapper.deleteByPrimaryKey(id);
    }


    /**
     * 小程序中根据valueId显示所有的评论,showType=0时，显示所有评论showType=1时显示有图的评论
     * @param info
     * @param valueId
     * @param showType
     * @param type  0表示商品评论   1表示专题评论
     * @return
     */
    @Override
    public CommonData<WxCommentListVo>  wxList(BasePageInfo info, Integer valueId,Integer showType,Integer type) {

        if (type==0){
            //开启分页
            PageHelper.startPage(info.getPage(),info.getLimit());
            //showType=0时，显示所有的评论
            if (showType==0){
                //根据valueId查询Comment的信息
                MarketCommentExample example = new MarketCommentExample();
                MarketCommentExample.Criteria criteria = example.createCriteria();
                criteria.andValueIdEqualTo(valueId);
                Byte a=0;
                criteria.andTypeEqualTo(a);
                List<MarketComment> list = marketCommentMapper.selectByExample(example);
                //创建响应的集合
                ArrayList<WxCommentListVo> data = new ArrayList<>();

                //遍历list
                for (MarketComment marketComment : list) {
                    //创建响应体中的对象
                    WxCommentListVo wxCommentListVo = new WxCommentListVo();
                    //创建响应体中userInfo对应的对象
                    WxUserInfoOfCommentVo wxUserInfoOfCommentVo = new WxUserInfoOfCommentVo();
                    //获取userId,根据userId去查
                    Integer userId = marketComment.getUserId();
                    MarketUser marketUser = marketUserMapper.selectByPrimaryKey(userId);
                    String nickname = marketUser.getNickname();
                    String avatar = marketUser.getAvatar();
                    wxUserInfoOfCommentVo.setNickname(nickname);
                    wxUserInfoOfCommentVo.setAvatarUrl(avatar);

                    //给响应对象赋值
                    wxCommentListVo.setUserInfo(wxUserInfoOfCommentVo);
                    wxCommentListVo.setAddTime(marketComment.getAddTime());
                    wxCommentListVo.setPicList(marketComment.getPicUrls());
                    wxCommentListVo.setAdminContent(marketComment.getAdminContent());
                    wxCommentListVo.setContent(marketComment.getContent());

                    //PageInfo<MarketComment> pageInfo = new PageInfo<>(list);
                    //响应对象  转给  响应集合
                    data.add(wxCommentListVo);
                }
                PageInfo<MarketComment> pageInfo = new PageInfo<>(list);
                return new CommonData<>(
                        (int) pageInfo.getTotal(),
                        pageInfo.getPages(),
                        info.getLimit(),
                        info.getPage(),
                        data
                );
            }else if (showType==1){
                //showType=1时，显示有图的评论
                MarketCommentExample example = new MarketCommentExample();
                MarketCommentExample.Criteria criteria = example.createCriteria();
                criteria.andValueIdEqualTo(valueId);
                //TODO  存疑点  不知道读过来的数据类型到底是什么
                criteria.andHasPictureEqualTo(true);
                List<MarketComment> list = marketCommentMapper.selectByExample(example);
                //创建响应的集合
                ArrayList<WxCommentListVo> data = new ArrayList<>();

                //遍历list
                for (MarketComment marketComment : list) {
                    //创建响应体中的对象
                    WxCommentListVo wxCommentListVo = new WxCommentListVo();
                    //创建响应体中userInfo对应的对象
                    WxUserInfoOfCommentVo wxUserInfoOfCommentVo = new WxUserInfoOfCommentVo();
                    //获取userId,根据userId去查
                    Integer userId = marketComment.getUserId();
                    MarketUser marketUser = marketUserMapper.selectByPrimaryKey(userId);
                    String nickname = marketUser.getNickname();
                    String avatar = marketUser.getAvatar();
                    wxUserInfoOfCommentVo.setNickname(nickname);
                    wxUserInfoOfCommentVo.setAvatarUrl(avatar);

                    //给响应对象赋值
                    wxCommentListVo.setUserInfo(wxUserInfoOfCommentVo);
                    wxCommentListVo.setAddTime(marketComment.getAddTime());
                    wxCommentListVo.setPicList(marketComment.getPicUrls());
                    wxCommentListVo.setAdminContent(marketComment.getAdminContent());
                    wxCommentListVo.setContent(marketComment.getContent());

                    //响应对象  转给  响应集合
                    data.add(wxCommentListVo);
                }
//            PageInfo<MarketComment> pageInfo = new PageInfo<>(list);
//            return data;
                PageInfo<MarketComment> pageInfo = new PageInfo<>(list);
                return new CommonData<>(
                        (int) pageInfo.getTotal(),
                        pageInfo.getPages(),
                        info.getLimit(),
                        info.getPage(),
                        data
                );
            }
        }
        //TODO type=1时，显示专题的评论信息
        //开启分页
        PageHelper.startPage(info.getPage(),info.getLimit());
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        Byte a=1;
        criteria.andTypeEqualTo(a);
        criteria.andValueIdEqualTo(valueId);
        List<MarketComment> list = marketCommentMapper.selectByExample(marketCommentExample);
        PageInfo<MarketComment> pageInfo = new PageInfo<>(list);

        //创建响应的集合
        ArrayList<WxCommentListVo> data = new ArrayList<>();

        //遍历list
        for (MarketComment marketComment : list) {
            //创建响应体中的对象
            WxCommentListVo wxCommentListVo = new WxCommentListVo();
            //创建响应体中userInfo对应的对象
            WxUserInfoOfCommentVo wxUserInfoOfCommentVo = new WxUserInfoOfCommentVo();
            //获取userId,根据userId去查
            Integer userId = marketComment.getUserId();
            MarketUser marketUser = marketUserMapper.selectByPrimaryKey(userId);
            String nickname = marketUser.getNickname();
            String avatar = marketUser.getAvatar();
            wxUserInfoOfCommentVo.setNickname(nickname);
            wxUserInfoOfCommentVo.setAvatarUrl(avatar);

            //给响应对象赋值
            wxCommentListVo.setUserInfo(wxUserInfoOfCommentVo);
            wxCommentListVo.setAddTime(marketComment.getAddTime());
            wxCommentListVo.setPicList(marketComment.getPicUrls());
            wxCommentListVo.setAdminContent(marketComment.getAdminContent());
            wxCommentListVo.setContent(marketComment.getContent());

            //PageInfo<MarketComment> pageInfo = new PageInfo<>(list);
            //响应对象  转给  响应集合
            data.add(wxCommentListVo);
        }
        PageInfo<MarketComment> pageInfo1 = new PageInfo<>(list);
        return new CommonData<>(
                (int) pageInfo1.getTotal(),
                pageInfo1.getPages(),
                info.getLimit(),
                info.getPage(),
                data
        );

    }

    /**
     * 统计商品评论的数量。分为所有评论和有图的评论
     * 根据hasPicture进行判断
     * @param valueId
     * @param type
     * @return
     */
    @Override
    public WxCommentCountVo count(Integer valueId, Byte type) {
        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        criteria.andTypeEqualTo(type);
        List<MarketComment> list = marketCommentMapper.selectByExample(example);
        int allCount=list.size();
        //有图的数量
        int hasPicCount=0;
        for (MarketComment marketComment : list) {
            Boolean hasPicture = marketComment.getHasPicture();
            //TODO  存疑
            if (hasPicture==true){
                hasPicCount++;
            }
        }
        //创建响应的对象
        WxCommentCountVo wxCommentCountVo = new WxCommentCountVo();
        wxCommentCountVo.setAllCount(allCount);
        wxCommentCountVo.setHasPicCount(hasPicCount);
        return wxCommentCountVo;
    }

    /**
     * 专题提交的评论
     * @param marketComment
     * @return
     */
    @Override
    public MarketComment wxPost(MarketComment marketComment) {
        //获取当前时间
        Date date = new Date();
        marketComment.setAddTime(date);
        marketComment.setUpdateTime(date);
        marketCommentMapper.insertSelective(marketComment);
        Integer id = marketComment.getId();
        MarketComment data = marketCommentMapper.selectByPrimaryKey(id);
        return data;
    }
}

